<?php

namespace app\admin\business;

use app\common\model\mysql\MallAdminUser as adminUserModel;
// 业务逻辑层
class MallAdminUser
{
	public function login($data){
		// 实例化用户model对象
		$objAdminUser = new adminUserModel();
		$adminUser = $objAdminUser->getAdminUserByUsername($data['username']);

		if(empty($adminUser) || $adminUser->status != config('status.mysql.normal')){
			//return show(config('status.error'),"不存在该用户");
			throw new \think\Exception("不存在该用户");
			
		}
		$adminUser = $adminUser->toArray();

		// 判断密码是否正确
		if($adminUser['password'] != md5($data['password'] )){
			//return show(config('status.error'),"用户密码错误");
			throw new \think\Exception("用户密码错误");
			
		}

		$data = [
			'update_time' => time(),
			'last_login_time' => time(),
			'last_login_ip' => $this->request->ip(),
		];
		$res = $objAdminUser->updateAdminUserByUserId($adminUser['id'],$data);
		if(empty($res)){
			//return show(config('status.error'),"更新失败");
			throw new \think\Exception("登录失败");
		}
		// 记录session
		session(config('admin.session_admin'),$adminUser);

		return true;
	}
}

